package com.scrm.exam.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.scrm.exam.errorcode.ErrorCode;
import com.scrm.exam.exception.GzsendiException;
import org.springframework.lang.Nullable;

import java.util.Collection;

/**
 * @author xzq
 * @ClassName Assert.java
 * @Description 重写断言
 * @createTime 2021/6/17
 */
public class Assert extends org.springframework.util.Assert {


    /**
     * 空字符处理
     */
    public static void notBlank(String str, String errorMsg) {
        if (StrUtil.isBlank(str)) {
            throw new GzsendiException(errorMsg);
        }
    }

    /**
     * 空集合处理
     */
    public static void notEmpty(@Nullable Collection<?> collection, String errorMsg) {
        if (CollUtil.isEmpty(collection)) {
            throw new GzsendiException(errorMsg);
        }
    }

    /**
     * 空集合处理2
     */
    public static void notEmpty(@Nullable Collection<?> collection, ErrorCode errorCode) {
        if (CollUtil.isEmpty(collection)) {
            throw new GzsendiException(errorCode);
        }
    }

    /**
     * 空对象处理
     */
    public static void notNull(Object obj, String errorMsg) {
        if (ObjectUtil.isNull(obj)) {
            throw new GzsendiException(errorMsg);
        }
    }


    /**
     * 空对象处理
     */
    public static void notNull(Object obj, ErrorCode errorCode) {
        if (ObjectUtil.isNull(obj)) {
            throw new GzsendiException(errorCode);
        }
    }

    /**
     * false异常处理
     */
    public static void isTrue(boolean expression, String errorMsg) {
        if (!expression) {
            throw new GzsendiException(errorMsg);
        }
    }

    /**
     * false异常处理
     *
     * @param expression
     * @param errorCode
     */
    public static void isTrue(boolean expression, ErrorCode errorCode) {
        if (!expression) {
            throw new GzsendiException(errorCode);
        }
    }


    /**
     * true异常处理
     */
    public static void isFalse(boolean expression, String errorMsg) {
        if (expression) {
            throw new GzsendiException(errorMsg);
        }
    }

    /**
     * true异常处理
     */
    public static void isFalse(boolean expression, ErrorCode errorCode) {
        if (expression) {
            throw new GzsendiException(errorCode);
        }
    }


}
